<?php

namespace app\admin\controller\dataCenter;

use app\admin\model\Chat;
use app\admin\model\Staff;
use app\admin\model\UserPvLog;
use \app\common\Common;
use app\sys\service\OfficeExe;
use think\Db;
use think\Exception;
use think\facade\Validate;
use think\Request;
use \app\admin\model\User as UserModel;

class User extends Common
{
    public function userCardList(Request $request)
    {
        $data = $request->param();
        $validate = Validate::make([
            "mobile|手机号" => "mobile",
            "site|站点" => "chsAlphaNum",
            "gather_method|方式" => "chs",
            "date_arr|日期区间" => "array",
        ]);
        if (!$validate->check($data)) {
            return failedJson($validate->getError());
        }
        $page = $request->param("page", 0);
        $limit = $request->param("limit", 10);
        $f = "u.create_time,u.name,c.mobile,u.store_type
            ,u.address,u.area,u.site,u.source,u.url
            ,u.gather_method,u.staff_no,is.staff_name,ifNUll(u.remark,'') as remark,u.source_url";
        $list = UserModel::getUserList($f,$data, $page, $limit);
        return successJson("success", $list);
    }

    public function userCardListExcel(Request $request)
    {
        $data = $request->param();
        $validate = Validate::make([
            "mobile|手机号" => "mobile",
            "site|站点" => "chsAlphaNum",
            "gather_method|方式" => "chs",
            "date_arr|日期区间" => "array",
        ]);
        if (!$validate->check($data)) {
            return failedJson($validate->getError());
        }
        $page = $request->param("page", 0);
        $limit = $request->param("limit", 1000);
        $f = "u.create_time,u.name,c.mobile,u.site,u.source,u.source_url,u.url,u.gather_method,is.staff_name,ifNUll(u.remark,'') as remark";
        $list = UserModel::getUserList($f,$data, $page, $limit);

        $headArr = ["记录时间","姓名","电话","站点","访问来源","来源页面","采集URL","采集方式","采集人","备注"];
        return OfficeExe::getExcel($headArr,$list["list"]->toArray());
    }

    public function chatList(Request $request)
    {
        $data = $request->param();
        $validate = Validate::make([
            "name|名称" => "chsAlphaNum",
            "info|内容" => "chsAlphaNum",
            "date_arr|日期区间" => "array",
        ]);
        if (!$validate->check($data)) {
            return failedJson($validate->getError());
        }
        $pid = $request->param("pxs_id", "");
        $pm = $request->param("shop_mobile", "");
        $name = $request->param("name", "");
        $info = $request->param("info", "");
        $date_arr = $request->param("date_arr", []);
        $page = $request->param("page", 0);
        $limit = $request->param("limit", 10);
        $export = $request->param("export", false);

        $date_arr = GetStrToTime($date_arr);

        $list = Chat::ChatList($pid, $pm, $name, $info, $date_arr, $page, $limit, $export);
        # excel导出
        if ($export) {
            $exportData=[];
            $headArr = ["客户","开始时间","结束时间","店铺ID","店铺链接","接待人","现负责客服"];
            foreach ($list['list'] as $key=>$item){
                $exportData[$key]['user_name']=$item['user_name']??"";//客户
                $exportData[$key]['create_time']=$item['create_time']??"";//开始时间
                $exportData[$key]['update_time']=$item['update_time']??"";//结束时间
                $exportData[$key]['pxs_id']=$item['pxs_id']??"";//店铺ID
                $exportData[$key]['url']=$item['url']??"";//店铺链接
                $exportData[$key]['before_staff_name']=$item['before_staff_name']??"";//之前负责客服
                $exportData[$key]['staff_name']=$item['staff_name']??"";//现负责客服
            }
            return OfficeExe::getExcel($headArr,$exportData);
        }
        return successJson("success", $list);
    }

    public function chatLogList(Request $request)
    {
        $data = $request->param();
        $validate = Validate::make([
            "search|搜索" => "chsAlphaNum",
            "date_arr|日期区间" => "array",
        ]);
        $chatId = $request->param("chat_id", "");
        $date_arr = $request->param("date_arr", []);
        $page = $request->param("page", 0);
        $limit = $request->param("limit", 10);

        if (!$validate->check($data)) {
            return failedJson($validate->getError());
        }
        $list = Chat::chatLogList($chatId, $date_arr, $page, $limit);
        return successJson("success", $list);
    }

    public function chatTj(Request $request)
    {
        $site = $request->param("site", "");
        $date_arr = $request->param("date_arr", []);
        $page = $request->param("page", 0);
        $limit = $request->param("limit", 10);
        $list = Chat::chatTj($site,$date_arr,$page,$limit);
        return successJson("success", $list);
    }

    // 访客列表~
    public function visList(Request $request)
    {
        $mobile = $request->param("mobile", "");
        $site = $request->param("site", "");
        $no = $request->param("no", "");
        $sourceName = $request->param("source_name", "");
        $date_arr = $request->param("date_arr", [date('Y-m-d'),date('Y-m-d')]);
        $page = $request->param("page", 0);
        $limit = $request->param("limit", 10);
        $list = UserPvLog::vList($mobile,$site,$sourceName,$date_arr,$no,$page,$limit);
        return successJson("success", $list);
    }

    public function visListExcel(Request $request)
    {
        $mobile = $request->param("mobile", "");
        $site = $request->param("site", "");
        $no = $request->param("no", "");
        $sourceName = $request->param("source_name", "");
        $date_arr = $request->param("date_arr", [date('Y-m-d'),date('Y-m-d')]);
        $page = $request->param("page", 0);

        $limit = $request->param("limit", 1000);
        $list = UserPvLog::vList($mobile, $site, $sourceName, $date_arr, $no, $page, $limit);
        $headArr = ["访问时间", "访客ID", "地区", "站点", "访问来源", "上一链接", "初始链接"];
        return OfficeExe::getExcel($headArr, $list["list"]);
    }

    public function chatT(Request $request)
    {
        $type = $request->param("type", "");
        $date_arr = $request->param("date_arr", []);
        $page = $request->param("page", 0);
        $limit = $request->param("limit", 10);
        //有效数据 all返回全部数据 valid 返回不为0的数据
        $valid_data= $request->param("valid_data", 'all');
        if(empty($date_arr)){
            $date_arr = [date("Y-m-d"),date("Y-m-d")];
        }
        list($res,$all,$resC) = Chat::chatT($type,$date_arr,$valid_data,$page,$limit);
        return successJson("success", listTJDate($all,$res,$resC));
    }

    public function chatTExcel(Request $request)
    {
        $type = $request->param("type", "");
        $date_arr = $request->param("date_arr", []);
        $page = $request->param("page", 0);
        $limit = $request->param("limit", Staff::count());
        //有效数据 all返回全部数据 valid 返回不为0的数据
        $valid_data= $request->param("valid_data", 'all');
        if(empty($date_arr)){
            $date_arr = [date("Y-m-d"),date("Y-m-d")];
        }
        list($res,$all,$resC) = Chat::chatT($type,$date_arr,$valid_data,$page,$limit);

        if($type == "date"){
            $headArr = ["日期","客服在线时长","对话时长","平均对话时长","对话次数","有效对话次数","极佳对话","客服回复咨询","客服接入咨询","客服回复率"];
        }else{
            $headArr = ["客服","客服在线时长","对话时长","平均对话时长","对话次数","有效对话次数","极佳对话","客服回复咨询","客服接入咨询","客服回复率"];
        }

        array_unshift($res,$all);
        return OfficeExe::getExcel($headArr,$res);
    }
}